<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="图片名称" prop="bannerName">
        <el-input v-model="dataForm.bannerName" placeholder="图片名称"></el-input>
      </el-form-item>
      <!--TODO-->
      <el-form-item label="导航图片" prop="bannerUrl">
        <!--<el-input v-model="dataForm.bannerUrl" placeholder="图片路径"></el-input>-->
        <el-upload
          class="upload-demo"
          :action="url"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :on-success="successHandle"
          :file-list="fileList2"
          :limit="1"
          v-model="dataForm.bannerUrl"
          list-type="picture">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
        </el-upload>
      </el-form-item>
      <!--
      <el-form-item label="图片路径" prop="bannerUrl">
        <el-input v-model="dataForm.bannerUrl" placeholder="图片路径"></el-input>
      </el-form-item>
      <el-form-item label="图片大小" prop="bannerSize">
        <el-input v-model="dataForm.bannerSize" placeholder="图片大小"></el-input>
      </el-form-item>
      -->
      <el-form-item label="图片顺序" prop="bannerOrder">
        <el-input v-model="dataForm.bannerOrder" placeholder="图片顺序"></el-input>
      </el-form-item>
      <el-form-item label="图片状态" prop="bannerStatus">
        <el-input v-model="dataForm.bannerStatus" placeholder="图片状态"></el-input>
      </el-form-item>
      <el-form-item label="banner广告的链接地址" prop="bannerHrefUrl">
        <el-input v-model="dataForm.bannerHrefUrl" placeholder="banner广告的链接地址"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="bannerDesc">
        <el-input v-model="dataForm.bannerDesc" placeholder="描述"></el-input>
      </el-form-item>
      <el-form-item label="操作员" prop="operator">
        <el-input v-model="dataForm.operator" placeholder="操作员"></el-input>
      </el-form-item>
      <!--
      <el-form-item label="日期">
        <el-date-picker
          v-model="visitDate"
          type="daterange"

          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item label="创建即上传时间" prop="createUtc">
        <!--<el-input v-model="dataForm.createUtc" placeholder="创建即上传时间"></el-input>-->
        <el-date-picker
          v-model="dataForm.createUtc"

          format="yyyy-MM-dd HH:mm:ss"
          >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改即更新时间" prop="modifyUtc">
        <!--<el-input v-model="dataForm.modifyUtc" placeholder="修改即更新时间"></el-input>-->
        <el-date-picker
          v-model="dataForm.modifyUtc"

          format="yyyy-MM-dd HH:mm:ss"
        >
        </el-date-picker>
      </el-form-item>
      <!--<el-form-item label="是否删除" prop="isDeleted">
        <el-input v-model="dataForm.isDeleted" placeholder="是否删除"></el-input>
      </el-form-item>-->

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import isInteger from 'lodash/isInteger'
  import moment from 'moment'
  export default {
    data () {
      return {
        // 局部变量
        baseUrl: 'http://localhost:8080/renren-fast',
        fileListItem1Name: '',
        fileListItem1url: '',
        url: this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`),
        fileList2: [
          {
            name: '',
            url: ''
          }
//          ,
//          {
//            name: 'food2.jpeg',
//            url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
//          }
        ],
        value1: '',
        value2: '',
        visible: false,
        dataForm: {
          id: 0,
          bannerName: '',
          bannerUrl: '',
          bannerSize: '',
          bannerOrder: '',
          bannerStatus: '',
          bannerHrefUrl: '',
          bannerDesc: '',
          operator: '',
          createUtc: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          modifyUtc: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
          isDeleted: '',
          version: ''
        },
        dataRule: {
          bannerName: [
            { required: true, message: '图片名称不能为空', trigger: 'blur' }
          ],
          bannerUrl: [
            { required: true, message: '图片路径不能为空', trigger: 'blur' }
          ],
          bannerSize: [
            { required: true, message: '图片大小不能为空', trigger: 'blur' }
          ],
          bannerOrder: [
            { required: true, message: '图片顺序不能为空', trigger: 'blur' }
          ],
          bannerStatus: [
            { required: true, message: '图片状态不能为空', trigger: 'blur' }
          ],
          bannerHrefUrl: [
            { required: true, message: 'banner广告的链接地址不能为空', trigger: 'blur' }
          ],
          bannerDesc: [
            { required: true, message: '描述不能为空', trigger: 'blur' }
          ],
          operator: [
            { required: true, message: '操作员不能为空', trigger: 'blur' }
          ],
          createUtc: [
            { required: true, message: '创建即上传时间不能为空', trigger: 'blur' }
          ],
          modifyUtc: [
            { required: true, message: '修改即更新时间不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '是否删除不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      handleRemove (file, fileList) {
        console.log(file, fileList)
      },
      onChange (file, fileList) {
//        this.fileList = fileList
      },
      handlePreview (file) {
        console.log(file)
      },
      // 上传成功
      successHandle (response, file, fileList) {
//        this.fileList = fileList
//        this.fileList2[0].url = response.url
        this.successNum++
        if (response && response.code === 0) {
          this.dataForm.bannerUrl = response.url
          if (this.num === this.successNum) {
            this.$confirm('操作成功, 是否继续操作?', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).catch(() => {
              this.visible = false
            })
          }
        } else {
          this.$message.error(response.msg)
        }
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl('/generator/upkbanner/info' + (isInteger(this.dataForm.id) ? `/${this.dataForm.id}` : '')),
              method: 'get'
            }).then(({data}) => {
              console.log(data)
              console.log(data.upkBanner)
              if (data && data.code === 0) {
                this.dataForm.bannerName = data.upkBanner.bannerName
                this.dataForm.bannerUrl = data.upkBanner.bannerUrl
                this.dataForm.bannerSize = data.upkBanner.bannerSize
                this.dataForm.bannerOrder = data.upkBanner.bannerOrder
                this.dataForm.bannerStatus = data.upkBanner.bannerStatus
                this.dataForm.bannerHrefUrl = data.upkBanner.bannerHrefUrl
                this.dataForm.bannerDesc = data.upkBanner.bannerDesc
                this.dataForm.operator = data.upkBanner.operator
                this.dataForm.createUtc = data.upkBanner.createUtc
                this.dataForm.modifyUtc = data.upkBanner.modifyUtc
                this.dataForm.isDeleted = data.upkBanner.isDeleted
                this.dataForm.version = data.upkBanner.version
                this.fileList2 = [
                  {
                    url: 'http://localhost:8080/renren-fast' + this.dataForm.bannerUrl,
                    name: data.upkBanner.bannerName
                  }
                ]
//                this.fileList2[0].url = 'http://localhost:8080/renren-fast' + this.dataForm.bannerUrl
//                this.fileList2[0].name = data.upkBanner.bannerName
              }
            })
          } else {
            this.fileList2 = []
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'bannerName': this.dataForm.bannerName,
              'bannerUrl': this.dataForm.bannerUrl,
              'bannerSize': this.dataForm.bannerSize,
              'bannerOrder': this.dataForm.bannerOrder,
              'bannerStatus': this.dataForm.bannerStatus,
              'bannerHrefUrl': this.dataForm.bannerHrefUrl,
              'bannerDesc': this.dataForm.bannerDesc,
              'operator': this.dataForm.operator,
              'createUtc': this.dataForm.createUtc,
              'modifyUtc': this.dataForm.modifyUtc,
              'isDeleted': this.dataForm.isDeleted,
              'version': this.dataForm.version
            }
            var tick = !this.dataForm.id ? this.$http({
              url: this.$http.adornUrl('/generator/upkbanner/save'),
              method: 'post',
              data: this.$http.adornData(params)
            }) : this.$http({
              url: this.$http.adornUrl('/generator/upkbanner/update'),
              method: 'post',
              data: this.$http.adornData(params)
            })
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
